#!/bin/bash

# RK Vendor Storage Tools Installation Script
# Author: Zhang Zhen <zhangzhen@cmhi.chinamobile.com>

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE}  RK Vendor Storage Tools Installer            ${NC}"
echo -e "${BLUE}  Author: Zhang Zhen                           ${NC}"
echo -e "${BLUE}================================================${NC}"
echo

# Check if running as root
if [[ $EUID -ne 0 ]]; then
   echo -e "${RED}错误：此脚本需要root权限运行${NC}"
   echo "请使用: sudo $0"
   exit 1
fi

# Check if binaries exist
echo -e "${YELLOW}检查编译文件...${NC}"
if [ ! -f "./bin/WriteSN" ] || [ ! -f "./bin/YamlSN" ]; then
    echo -e "${RED}错误：找不到编译后的程序文件${NC}"
    echo "请先运行 'make all' 编译程序"
    exit 1
fi

echo -e "${GREEN}✓ 所有程序文件已找到${NC}"
echo

# Install using Makefile
echo -e "${YELLOW}正在安装RK Vendor Storage工具...${NC}"
make install

echo
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN}  安装完成！                                   ${NC}"
echo -e "${GREEN}================================================${NC}"
echo
echo -e "${BLUE}已安装的工具:${NC}"
echo "  • WriteSN            - 交互式TUI配置工具 (推荐)"
echo "  • YamlSN             - 开机服务程序"
echo
echo -e "${BLUE}使用方法:${NC}"
echo "  直接运行: sudo WriteSN"
echo
echo -e "${BLUE}开机服务安装:${NC}"
echo "  运行: ./scripts/install_boot_service.sh"
echo
echo -e "${BLUE}卸载工具:${NC}"
echo "  运行: sudo make uninstall"
echo
echo -e "${YELLOW}注意：现在可以在任何目录下直接运行 'WriteSN' 命令！${NC}"
